package squarePackage;

import java.math.BigDecimal;

import java.util.List;
import java.util.Random;

import be.kuleuven.cs.som.annotate.Basic;

import Enumerations.Directions;

/**
 * An class of teleporters for creating teleport transparant squares.
 * 
 * @invar   ...
 * 			|this.isValidDestinationList(this.getDestinationList()) == true;
 * 
 * @version  2.0
 * @author   An
 */
public class TeleportTransparantSquare extends TransparantSquare implements Teleport {

	/**
	 * A variable to store the destinations of this teleporter
	 */
	private List<SquareImp> destinationList;
	
	/**
	 * A constructor to initialize a transparant square that is also a teleporter
	 * 
	 * @param temperature
	 * @param humidity
	 * @param numberOfWalls
	 * @param direction
	 * @param list
	 */
	public TeleportTransparantSquare(Double temperature, BigDecimal humidity, int numberOfWalls, Directions direction,List<SquareImp> list){
		super(temperature, humidity, numberOfWalls, direction);
		this.setDestinationList(list);
	}
	
	/**
	 * A constructor to initialize a transparant square that is also a teleporter
	 * @param list
	 */
	public TeleportTransparantSquare(List<SquareImp> list){
		super();
		this.setDestinationList(list);
	}
	/**
	 * A method to get the destinationlist
	 */
	@Basic
	@Override
	public List<SquareImp> getDestinationList() {
		return destinationList;
	}
	
	/**
	 * A method to set the destinationlist
	 *@see Teleport
	 */
	@Override
	public void setDestinationList(List<SquareImp> destinationList) {
		if (this.isValidDestinationList(destinationList))
			this.destinationList = destinationList;
	}

	/**
	 * @see Teleport
	 */
	@Override
	public boolean isValidDestinationList(List <SquareImp> destinationList) {
		for (SquareImp square : destinationList){
			if (!this.isValidDestination(square)){
				return false;
			}
		}
		return true;
		
	}

	/**
	 * @see Teleport
	 */
	@Override
	public Square chooseRandomDestination() {
		Random random = new Random();
		int i = random.nextInt(this.getDestinationList().size());
		while (this.getDestinationList().get(i) == null){
			i = random.nextInt(this.getDestinationList().size());
		}
		return this.getDestinationList().get(i);
		
	}

	/**
	 * @see Teleport
	 */
	@Override
	public void setDestination(SquareImp square) {
		if (this.isValidDestination(square) && !this.hasDestination(square))
			this.destinationList.add(square);
		
	}

	/**
	 * @see Teleport
	 */
	@Override
	public boolean isValidDestination(SquareImp square) {
		return (!(square instanceof RockSquare) && square != null && !square.equals(this));
	}


	/**
	 * @see Teleport
	 */
	@Override
	public boolean hasDestination(SquareImp destination) {
		return this.getDestinationList().contains(destination);
	}


}